<?php
namespace app\web\controller;

use app\model\Theme; 

class LtAddoreditltitem extends Base
{
    public function Lt_addoreditltitem()
    {
/**
 * Created by 抽奖大转盘
 * Desc:奖项增加修改操作
 * User:cbw
 * Date: 2020/11/29
 */

global $_G,$_P;
//load_func('logging');
$response = modelShow(40000);
if (!$_G['ispost']){
    $response = modelShow(50000);
}else{
    $params = $_P;
    $ltt_id = isset($params['ltt_id'])?$params['ltt_id']:'';
    $themeid = isset($params['themeid'])?$params['themeid']:'';
    $theme = Theme::getThemeById($themeid);
    if (!$theme){
        $response = modelShow(40002);
        return_msg($response, '', 'ajax');
    }
    $ltt_title = isset($params['ltt_title'])?trim($params['ltt_title']):'';
    $ltt_qty = isset($params['ltt_qty'])?intval($params['ltt_qty']):0;
    $ltt_ratiio = sprintf("%.2f",floatval($_P['ltt_ratiio']));
    $ltt_showratiio = sprintf("%.2f",floatval($_P['ltt_showratiio']));
//    $ltt_other_title = isset($params['ltt_other_title'])?trim($params['ltt_other_title']):'';
    $ltt_other_title = '';
    $ltp_name = isset($params['ltp_name'])?trim($params['ltp_name']):'';
    $ltp_img = $params['ltp_img']?$params['ltp_img']:'';
    $ltp_detail_imgs = $params['centerimgs']?$params['centerimgs']:'';
    $receive_key = isset($params['receive_key_id'])?$params['receive_key_id']:0;
    $receive_todo_num = isset($params['receive_todo_num'])?$params['receive_todo_num']:0;
    $it_type = isset($params['it_type'])?$params['it_type']:1;
    $sort = isset($params['sort'])?$params['sort']:1;
    $page_total = intval(db_find("select count(*) as cnt from".tablename(t_lottery))['cnt']);
//    || !$ltt_other_title

    if (!$themeid || !$ltt_title || (!$ltp_name && $it_type!=2)  || !$ltt_ratiio  || !$ltp_img  ){
        $response = modelShow(40001);
    }else{
        if($receive_key>0 && $receive_todo_num==0){
            $response = modelShow(40029);
        }else{
            try {
                $data = [
                    'themeid'=>$themeid,
                    'ltt_title'=>$ltt_title,
                    'ltt_qty'=>$ltt_qty,
                    'ltt_ratiio'=>$ltt_ratiio*100,
                    'ltt_showratiio'=>$ltt_showratiio*100,
                    'sort'=>$sort,
                    'ltt_other_title'=>$ltt_other_title,
                    'ltp_name'=>$ltp_name,
                    'ltp_img'=>tomedia(str_replace('/99.jpg','/logo.jpg',$ltp_img)),
                    'ltp_detail_imgs'=>serialize($ltp_detail_imgs),
                    'updated_at'=>date("Y-m-d H:i:s"),
                    'op_uid'=>$_G['user']['uid'],
                    'receive_key'=>$receive_key,
                    'receive_todo_num'=>$receive_todo_num,
                    'it_type'=>$it_type,
                ];
                if (!$ltt_id){
                    if ($page_total<7){
                        $res = $insertid = db_insert(t_lottery,$data);
                    }else{
                        $response = modelShow(40060);
                    }
                }else{
                    $where = ['ltt_id'=>$ltt_id];
                    $res = db_update(t_lottery,$data,$where);
                }

                if (!$res){
                    $response = modelShow(30000);
                }else{
                    $response = modelShow(200);
                }
            }catch (Exception $e) {
                logging_run('错误日志:addOrEditLtItem[参数：'.json_encode($params).'-错误信息:'.$e->getMessage());
                $response = modelShow(200,$e->getMessage(),[]);
            }
        }

    }
}
return_msg($response, '', 'ajax');
}
}